package com.android.vspice;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.TableRow.LayoutParams;

import com.android.vspice.swig.Element;

public class ElementSetting extends Activity {
	Spinner spinType;
	EditText textName;
	TableLayout layoutTable;
	Button btnOK;
	Button btnCancel;
	Element element;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.parameter);
		
		Intent i = getIntent();
		int index = i.getExtras().getInt("index");
		//element = ElementList.getInstance().getItem(index);
		
		layoutTable = (TableLayout)findViewById(R.id.layout_eletable);
		spinType = (Spinner)findViewById(R.id.spinner_type);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.elements, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(
				android.R.layout.simple_spinner_dropdown_item);
		spinType.setAdapter(adapter);
		spinType.setOnItemSelectedListener(new SpinnerItemListener());
		
		textName = (EditText)findViewById(R.id.edit_name);
		btnOK = (Button)findViewById(R.id.button_ok);
		btnCancel = (Button)findViewById(R.id.button_cancel);
		
		btnOK.setOnClickListener(listenOK);
		btnCancel.setOnClickListener(listenCancel);
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	private OnClickListener listenOK = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			String strName = textName.getText().toString();
			//String strParam = ;
		}
	};
	
	private OnClickListener listenCancel = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			finish();
		}
		
	};
	
	private void makeParamRow(int pos) {
		int count = layoutTable.getChildCount();
		
		if(count >= 3) {
			layoutTable.removeViews(2, count - 2);
		}
		
		if(pos == 0) {
			// Resistor
			
			textName.setText("R");
			
			final int rows = 3;
			String[] strText = {"Node1", "Node2", "Value"};
			TableRow[] row = new TableRow[rows];
			TextView[] text = new TextView[rows];
			EditText[] edit = new EditText[rows];
			LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LayoutParams weight_param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 1.0f);
			
			for(int i = 0 ; i < rows ; i++) {
				text[i] = new TextView(this);
				text[i].setLayoutParams(param);
				text[i].setText(strText[i]);
				
				edit[i] = new EditText(this);
				edit[i].setLayoutParams(weight_param);
				
				row[i] = new TableRow(this);
				row[i].setLayoutParams(param);
				row[i].addView(text[i]);
				row[i].addView(edit[i]);	
				
				layoutTable.addView(row[i], param);
			}			
		}
		else if(pos == 1) {
			
			textName.setText("C");
			
			// Capacitor
			final int rows = 4;
			String[] strText = {"Node +", "Node -", "Value", "IC"};
			TableRow[] row = new TableRow[rows];
			TextView[] text = new TextView[rows];
			EditText[] edit = new EditText[rows];
			LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LayoutParams weight_param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 1.0f);
			
			for(int i = 0 ; i < rows ; i++) {
				text[i] = new TextView(this);
				text[i].setLayoutParams(param);
				text[i].setText(strText[i]);
				
				edit[i] = new EditText(this);
				edit[i].setLayoutParams(weight_param);
				
				row[i] = new TableRow(this);
				row[i].setLayoutParams(param);
				row[i].addView(text[i]);
				row[i].addView(edit[i]);	
				
				layoutTable.addView(row[i], param);
			}				
		}
		else if(pos == 2) {
			
			textName.setText("L");
			
			// Inductor
			final int rows = 4;
			String[] strText = {"Node +", "Node -", "Value", "IC"};
			TableRow[] row = new TableRow[rows];
			TextView[] text = new TextView[rows];
			EditText[] edit = new EditText[rows];
			LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LayoutParams weight_param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 1.0f);
			
			for(int i = 0 ; i < rows ; i++) {
				text[i] = new TextView(this);
				text[i].setLayoutParams(param);
				text[i].setText(strText[i]);
				
				edit[i] = new EditText(this);
				edit[i].setLayoutParams(weight_param);
				
				row[i] = new TableRow(this);
				row[i].setLayoutParams(param);
				row[i].addView(text[i]);
				row[i].addView(edit[i]);	
				
				layoutTable.addView(row[i], param);
			}				
		} else if(pos == 3) {
			
			textName.setText("V");
			
			// Inductor
			final int rows = 4;
			String[] strText = {"Node +", "Node -", "DC", "Value"};
			TableRow[] row = new TableRow[rows];
			TextView[] text = new TextView[rows];
			EditText[] edit = new EditText[rows];
			LayoutParams param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT);
			LayoutParams weight_param = new LayoutParams(LayoutParams.MATCH_PARENT,
					LayoutParams.WRAP_CONTENT, 1.0f);
			
			for(int i = 0 ; i < rows ; i++) {
				text[i] = new TextView(this);
				text[i].setLayoutParams(param);
				text[i].setText(strText[i]);
				
				edit[i] = new EditText(this);
				edit[i].setLayoutParams(weight_param);
				
				row[i] = new TableRow(this);
				row[i].setLayoutParams(param);
				row[i].addView(text[i]);
				row[i].addView(edit[i]);	
				
				layoutTable.addView(row[i], param);
			}			
		}
	}
	
	private class SpinnerItemListener implements OnItemSelectedListener 
	{
		@Override
		public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub
			makeParamRow(arg2);
		}

		@Override
		public void onNothingSelected(AdapterView<?> arg0) {
			// TODO Auto-generated method stub
			
		}
	}
}
